package com.bw.controller;

import com.bw.entity.*;
import com.bw.mapper.HouseMapper;
import com.bw.mapper.RegionMapper;
import com.bw.service.HouseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.spring.kun.common.TokenUtils;
import org.spring.kun.common.UploadPicUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.swing.plaf.synth.Region;
import javax.validation.Valid;
import java.util.List;

@Controller
public class ListController {


    @Autowired
    RegionMapper regionMapper;

    @Autowired
    HouseMapper houseMapper;
    @Autowired
    HouseService houseService;

    @RequestMapping("/list.do")
    public String list(HouseQuery query,Integer pageNum, HttpServletRequest request, Model model){
        //登录用的
        User user = TokenUtils.getLoginUserFromRequest(request, User.class);
        model.addAttribute("name",user.getName());
        model.addAttribute("nickName",user.getNickName());
        model.addAttribute("avatarUrl",user.getAvatarUrl());

        //1. 初始化下拉框
        List<Dict> provinceList = regionMapper.provinceList(); //只初始化省份， 城市必须要选择省份联动才能初始化
        model.addAttribute("provinceList",provinceList);
        //2. 分页查询
        PageHelper.startPage(pageNum == null ? 1:pageNum,3);
       List<HouseInfo> list =  houseMapper.selectList(query);
        PageInfo<HouseInfo> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",pageInfo);

        //3. 条件回显
        model.addAttribute("query",query);
        if (query.getProvinceId() != null){
            List<Dict> cityList = regionMapper.cityListByProvinceId(query.getProvinceId());
            model.addAttribute("cityList",cityList);
        }
        return "list";
    }


    @RequestMapping("/selectCity.do")
    @ResponseBody
    public List<Dict> selectCity(Integer provinceId){
       return regionMapper.cityListByProvinceId(provinceId);
    }

    @RequestMapping("/addPage.do")
    public String addPage(Model model){
        List<Dict> provinceList = regionMapper.provinceList(); //只初始化省份， 城市必须要选择省份联动才能初始化
        model.addAttribute("provinceList",provinceList);
        return "add";
    }
    @RequestMapping("/add.do")
    public String add(@Valid HouseAdd houseAdd, BindingResult result, Model model,HttpServletRequest request){

        if (result.hasErrors()){
            model.addAttribute("error",result.getAllErrors().get(0).getDefaultMessage());
            List<Dict> provinceList = regionMapper.provinceList(); //只初始化省份， 城市必须要选择省份联动才能初始化
            model.addAttribute("provinceList",provinceList);
            return "add";
        }
        // 处理图片
        String url = UploadPicUtils.upload(houseAdd.getPicFile(), request);
        houseAdd.setPicUrl(url);
        // 执行添加
        houseService.add(houseAdd);
        return "redirect:list.do";
    }
}
